
//字数限制 100
window.wordLeg = function (obj) {
    var value = $(obj).val();
    var currleg = value.length;
    var length = 100;
    if (currleg > length) {
        layer.msg('字数请在' + length + '字以内');
        $(obj).val(value.substr(0,100));
    }
};
 
//字数限制 300
 window.wordAreaLeg = function (obj) {
       var value = $(obj).val();
       var currleg = value.length;
       var length = 300;
       if (currleg > length) {
            layer.msg('字数请在' + length + '字以内');
            $(obj).val(value.substr(0,300));
       }
  };
 
  
//字数限制 1000
window.wordUltraAreaLeg = function (obj) {
        var value = $(obj).val();
        var currleg = value.length;
        var length = 1000;
        if (currleg > length) {
             layer.msg('字数请在' + length + '字以内');
             $(obj).val(value.substr(0,1000));
        }
};

/**
 *对Date的扩展，将 Date 转化为指定格式的String
 *月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符，
 *年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
 *例子：
 *(new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
 *(new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18
 */
Date.prototype.format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

//使用字典表做下拉框绑定
var select4layui = {
	elem : null,
	obj : null,
	selected:null,
	init : function(config) {
		this.elem = $(config.elem);
		this.obj = config.obj;
		this.selected = config.selected;
		// 这边范围对应的对象，可以实现链式调用
		return this;
	},
	// 渲染元素
	render : function(layui) {
		var o = this.obj;
		var selected = this.selected;
		var content = '';
		if(selected){
			o.forEach(function(value, index) {
				if(value.dictValue==selected){
					content+='<option value="'+value.dictValue+'" selected>'+value.dictLabel+'</option>'
				}else{
					content+='<option value="'+value.dictValue+'">'+value.dictLabel+'</option>'
				}
			})
		}else{
			o.forEach(function(value, index) {
				content += '<option value="' + value.dictValue + '">'+ value.dictLabel + '</option>'
			})
		}
		this.elem.html(content);
		if(layui){
			layui.form.render();
		}else{
			console.log('layui is not null!');
		}
	}
};

//使用多图上传组件
var upload4layui = {
	div:null,
	elem : null,
	elemClear : null,
	elemPre: null,
	name: null,
	url: null,
	init: function(config){
		var num = Math.ceil(Math.random()*100);
		this.div = $(config.id);
		this.name = config.name;
		this.elem = "imgUpload"+num;
		this.elemClear = "clearUpload"+num;
		this.elemPre = "preImages"+num;
		this.url = config.url;
		var content="";
		content+=' <button type="button" class="layui-btn" id="'+this.elem+'">图片上传</button>';
		content+=' <button type="button" class="layui-btn layui-btn-danger" id="'+this.elemClear+'">清空</button>';
		content+=' <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">预览图：';
		content+=' <div class="layui-upload-list" id="'+this.elemPre+'" required></div>';
		content+=' </blockquote>';
		this.div.html(content);
		var _this = this;
		//绑定清空事件
		$("#"+this.elemClear).click(function(){
			$('#'+_this.elemPre).empty();
		   	//$('#'+_this.elemPre).html('<input type="hidden" name="'+_this.name+'" value="" />')
		   	layer.msg('图片已清空！');
	    });
		return this;
	},
	render : function(upload) {
		var elem = this.elem;
		var elemPre = this.elemPre;
		var name = this.name;
		// 多图片上传
	    upload.render({
	      elem: '#'+elem
	      ,url: rootPath + '/FileUtilController/upload/'
		  ,accept: 'file'  //普通文件
		  ,exts: 'jpg|png' //只允许上传图片
	      ,multiple: true  //支持多个
	      ,before: function(obj){
	        // 预读本地文件示例，不支持ie8
	        obj.preview(function(index, file, result){
	          $('#'+elemPre).append('<img style="width:200px;height:200px" src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img">')
	        });
	      }
	      ,done: function(res){
	        // 上传完毕
	        if(res.code==200){
	       	  layer.msg('图片上传成功！');
	       	  $('#'+elemPre).append('<input type="hidden"  name="'+name+'" value="'+res.data.id+'" />')
	        }else{
	       	  layer.msg('图片上传失败,请再次上传！');
	        }
	      }
	    });
	},
	setImages: function(images){
		var elemPre = this.elemPre;
		var name = this.name;
		if(images!=''){
			images.split(",").forEach(function(value , index , array){
				if(value!=""){
					$('#'+elemPre).append('<input type="hidden" name="'+name+'" value="'+value+'" /><img style="width:200px;height:200px" src="../../FileController/viewImg/'+value+'">')
				}
			})
		}
	}
	
};




$.extend({
	confirm: function(title,callback){
		//询问框
		layer.confirm(title, {
		  btn: ['确定','取消'] //按钮
		}, function(){
			callback()
		});
    },
    postajax:function(_url,_data,callback){
		if(!_data){
			_data = {pageNum: 1,pageSize: 10}
		}
	    $.ajax({
	        url: _url,
	        dataType: 'json',
	        cache: false,
	        type: 'post',
	        data: _data,
	        success:function(res, status){
	             callback(res,status);
	        },
	        error:function(data){
	        	callback(data);
	        }
	    });
    },
    getajax:function(_url,_data,callback){
    	if(!_data){
			_data = {pageNum: 1,pageSize: 10}
		}
	    $.ajax({
	        url: _url,
	        dataType: 'json',
	        cache: false,
	        type: 'get',
	        data: _data,
	        success:function(res, status){
	             callback(res,status);
	        },
	        error:function(data){
	        	callback(data);
	        }
	    });
    },
    success: function(msg){
    	layer.msg(msg, {
            icon : 1,
            shift : 0, //抖动效果
            time : 3000
        });
    },
    error: function(msg){
    	layer.msg(msg, {
            icon : 2,
            shift : 6, //抖动效果
            time : 3000
        });
    },
    confirm: function(title,callback){
    	layer.confirm(title, {
    	  	btn: ['确定','取消'] //按钮
    	}, callback);
    }
    
})

